﻿#include <QApplication>
#include <QQmlApplicationEngine>
#include "gametablemodel.h"
#include "hdataquery.h"
#include "mainwindow.h"
#include <QSqlDatabase>
#include <QTranslator>

void openDb()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(qApp->applicationDirPath()+"/"+"localStorage.db");
    bool ok = db.open();
    if (!ok)
        qApp->exit();
}

void loadTr()
{
    QTranslator *translator = new QTranslator(qApp);
    if (translator->load(":/translations/dataSource.qm"))
        qApp->installTranslator(translator);
}

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

#if 1
    QApplication app(argc, argv);
    openDb();
    loadTr();

    //! [registertype]
    qmlRegisterType<GameTableModel>("GameTableModel", 1, 0, "GameTableModel");
    qmlRegisterType<HDataQuery>("HDataQuery", 1, 0, "HDataQuery");
    //! [registertype]
    //!
    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
#endif

#if 0
    QApplication a(argc, argv);
    MainWindow m;
    m.show();
    return a.exec();
#endif
}
